<template>
  <div class='manage-departs'>
    <p class="title">{{title}}</p>
    
    <div class="btns-b">
      <el-button v-for="(item,index) in btnsData" :key="index" v-show="item.show" :type="item.type" class="btn-add fr" @click="item.click(item)">{{item.label}}</el-button>
      <el-upload class="upload-demo" ref="upload" accept=".xls,.xlsx"  name='file' :auto-upload="true" :action="filePath" :on-success="handleChange" :show-file-list="false">
        <el-button size="small" type="primary">上传</el-button>
      </el-upload>
    </div>

    <div class="table-box">
      <Table :modelData="modelData" :listData="listData" :pageInfo="pageInfo"></Table>
    </div>
  </div>
</template>
<script>
import Table from '../../components/table'
export default {
  components: {Table},
  name: "",
  data() {
    return {
      title:"部门投票管理",
      filePath:PATH+"department/importDept.do",
      btnsData:[
        {label:"投票停止",show:false,val:0, type:'success',click:this.end},
        {label:"投票开启",show:false,val:1, type:'success',click:this.start},
        {label:"新增",show:true, type:'primary',click:this.add},
        {label:"模板",show:true, type:'primary',click:this.download},
      ],
      modelData:[
        {myType:'index',label:"序号"},
        {myType:'normal',prop:"name",label:'部门'},
        {myType:'img',prop:"picture",label:'头像'},
        {myType:'operate',label:'管理',btns:[
          {label:'编辑',click:this.edit,icon:require('../../assets/images/manageHome/edit.png')},
          // {label:'删除',click:this.delete,icon:require('../../assets/images/manageIssues/delete.png')}
        ],width:"100"}
      ],
      listData:[],
      pageInfo:{
        page:1,
        total:10,
        size:10,
        changePage:this.changePage
      }
    };
  },
  methods: {
    // 导入
    import() {
      document.querySelector(".upload-demo").click();
    },
    // 开始
    start(item){
      this.startAndEnd(item.val)
    },
    // 停止
    end(item){
      this.startAndEnd(item.val)
    },
    download(){
      console.log("下载模板")
      location.href=downloadPath +"department/exportDept.do"
    },

    handleChange(res){
      _g.toMessage(res)
      this.getListData()
    },

    startAndEnd(val){
      _g.apiPost('meeting/switchVote.do',{votingStatus:val}).then(res=>{
        _g.toMessage(res)
        if(!res.status){
          this.btnsData.forEach((ele,index)=>{
            ele.show=index!==val //0关闭 1开启
          })
        }
      })
    },
    
    // 新增
    add() {
      this.$store.state.add.show = true;
      this.$store.state.add.OK=this.saveEdit
    },
    edit(scope) {
      this.$store.state.add.info=JSON.parse(JSON.stringify(scope.row))
      this.$store.state.add.OK=this.saveEdit
      this.$store.state.add.show = true;
    },
    saveEdit(){
      var params=this.$store.state.add.info
      delete params.createTimeStr
      _g.apiPost('department/insertOrUpdate.do',params).then(res=>{
         _g.toMessage(res)
         if(!res.status){
           this.getListData()
         }
      })
      console.log(params)
    },
    // 删除
    delete(scope) {
      this.$store.state.confirmInfo.message = "是否确认删除？";
      this.$store.state.confirmInfo.show = true;
    },
    // 翻页
    changePage() {
      this.getListData()
    },

    getListData(){
      var params = {
        currPageNo:this.pageInfo.page
      }
      _g.apiPost('department/init.do',params).then(res=>{
        this.pageInfo.total=res.data.total
        this.$set(this,'listData',res.data.list)
      })

      _g.apiPost('meeting/getMeetingInfo.do').then(res=>{
        this.btnsData.forEach((ele,index)=>{
          ele.show=index!==res.data.votingStatus //0关闭 1开启
        })
      })
    }
  },
  created() {
    this.getListData()
  }
};
</script>
<style lang="scss" scoped>
.manage-departs {
  width: 100%;
  height: 100%;
  padding: 10px;
  box-sizing: border-box;
  overflow: auto;

  .title {
    font-weight: 600;
    font-size: 20px;
    background-color: #fff;
    text-indent: 20px;
    height: 60px;
    line-height: 60px;
    box-sizing: border-box;
    border-bottom: 1px solid #ebeef5;
  }
  .upload-demo {
    float: right;
    width: 90px;
    height: 40px;
    line-height: 40px;
    margin-right: 20px;
    margin-top: 12.5px;
  }
  .btns-b {
    position: absolute;
    right: 20px;
    top: 7.5px;
    z-index: 100;
  }
  .table-box {
    height: calc(100% - 60px);
  }
}
</style>